package org.goldenport.g3.events

import com.asamioffice.goldenport.util.OptionUtil.lift
import com.asamioffice.goldenport.text.StringUtil.makeShortString
import org.goldenport.g3.{G3Event, G3Message}
import org.goldenport.g3.messages.{Record, RecordSet}

/**
 * @since   Aug. 10, 2010
 * @version Oct. 24, 2010
 * @author  ASAMI, Tomoharu
 */
abstract class RestEvent(
  uri: String,
  val content: Option[AnyRef] = None,
  cause: Option[G3Event] = None
) extends G3Event(uri, cause) {
  require (uri != null)

  def asRecord: Record = {
    content match {
      case Some(r: Record) => r
      case _ => Record()
    }
  }

  def asRecords: RecordSet = {
    content match {
      case Some(rs: RecordSet) => rs
      case Some(r: Record) => RecordSet(r)
      case _ => RecordSet()
    }
  }

  override def toString() = {
    "%s(%s, %s)".format(getClass.getSimpleName, uri, makeShortString(content))
  }
}

/*
class Getted(uris: List[String], val contents: List[AnyRef]) extends G3Event(uris(0)) {
  def this(uri: String, content: AnyRef) = this(List(uri), List(content))
}
*/

object Getted {
  def apply(uri: String, content: AnyRef = null) = {
    new Getted(uri, lift(content))
  }
}

object Posted {
  def apply(uri: String, content: AnyRef = null) = {
    new Posted(uri, lift(content))
  }
}

object Putted {
  def apply(uri: String, content: AnyRef = null) = {
    new Putted(uri, lift(content))
  }
}

object Deleted {
  def apply(uri: String, content: AnyRef = null) = {
    new Deleted(uri, lift(content))
  }
}

class Getted(uri: String,
             content: Option[AnyRef] = None,
             cause: Option[G3Event] = None
           ) extends RestEvent(uri, content, cause)

class Posted(uri: String,
             content: Option[AnyRef] = None,
             cause: Option[G3Event] = None
           ) extends RestEvent(uri, content, cause)

class Putted(uri: String,
             content: Option[AnyRef] = None,
             cause: Option[G3Event] = None
           ) extends RestEvent(uri, content, cause)

class Deleted(uri: String,
              content: Option[AnyRef] = None,
              cause: Option[G3Event] = None
            ) extends RestEvent(uri, content, cause)

class Mkcoled(uri: String,
              content: Option[AnyRef] = None,
              cause: Option[G3Event] = None
            ) extends RestEvent(uri, None, cause)
